/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
import * as data_block from "../data/block";
/*
 * 蛋白石组件
 */
import * as realm_property from "../system/realm_property";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 植树造木 < 50 能量消耗 >
 */
export function Forestry(block: server.Block) {
    // 判断能量值 是否足够
    if (!realm_property.ExpendEnergy(block, -50)) return;
    /**
     * * 获取 方块
     */
    const target = block.south();
    if (!target || !target.isValid()) return;
    /**
     * * 测试 方块类型
     */
    const test = data_block.is_wood.has(target.typeId);
    if (!test) return;
    switch (target?.typeId) {
        // 黑橡木
        case 'minecraft:dark_oak_log': native_tools.TryFillBlocks(block.dimension, math_tools.Vector.add(block, { x: 1, y: 0, z: 2 }), math_tools.Vector.add(block, math_tools.Vector.CONSTANT_SOUTH), 'minecraft:dark_oak_sapling'); break;
        // 橡木
        case 'minecraft:oak_log': target.setPermutation(server.BlockPermutation.resolve('minecraft:oak_sapling')); break;
        // 云杉
        case 'minecraft:spruce_log': target.setPermutation(server.BlockPermutation.resolve('minecraft:spruce_sapling')); break;
        // 白桦
        case 'minecraft:birch_log': target.setPermutation(server.BlockPermutation.resolve('minecraft:birch_sapling')); break;
        // 丛林
        case 'minecraft:jungle_log': target.setPermutation(server.BlockPermutation.resolve('minecraft:jungle_sapling')); break;
        // 金合欢
        case 'minecraft:acacia_log': target.setPermutation(server.BlockPermutation.resolve('acacia_sapling')); break;
        // 樱花树
        case 'minecraft:cherry_log': target.setPermutation(server.BlockPermutation.resolve('minecraft:cherry_sapling')); break;
        // 绯红菌柄
        case 'minecraft:crimson_stem': target.setPermutation(server.BlockPermutation.resolve('minecraft:crimson_fungus')); break;
        // 诡异菌柄
        case 'minecraft:warped_stem': target.setPermutation(server.BlockPermutation.resolve('minecraft:warped_fungus')); break;

        default: break;
    };
    /**
     * * 定义 起始点
     */
    const start = math_tools.Vector.add(block, { x: -7, y: -1, z: -6 });
    /**
     * * 定义 结束点
     */
    const done = math_tools.Vector.add(block, { x: 7, y: 19, z: 8 });
    /** 
     * * 在 绘制路径 时 执行 的 程序
     */
    const moveEvent = (args: data_format.ROUTE_ANNEX_ARGS) => {
        /** 
         * * 检测方块是否需要被挖掘
         */
        const TestSort = () => {
            /** 
             * * 获取 方块对象
             */
            const getBlock = args.dimension.getBlock(args.location);
            // 检测 方块类型
            if (getBlock) return data_block.is_trees.has(getBlock.typeId);
            else return false
        }
        //执行路径事件的功能
        if (TestSort()) args.dimension.runCommand(`fill ${args.location.x} ${args.location.y} ${args.location.z} ${args.location.x} ${args.location.y} ${args.location.z} air [] destroy`)
        // 继续循环
        return true
    };
    // 创建 路径执行计划
    achieve_plan.PathExecute.CreateForCube(
        '植树造木-范围扫描',
        {
            particles: ['constant:track_rune_green'],
            dimension: block.dimension,
            location_group: [],
            cooldown: 1,
            speed: 1,
            offset: math_tools.Vector.CONSTANT_HALF,
            on_move: moveEvent
        },
        start, done, 0.5
    );
};

/**
 * * 作物侦测 < 5 能量消耗 >
 */
export function Detection(object: server.Block) {
    // 判断能量值 是否足够
    if (!realm_property.ExpendEnergy(object, -5)) return;
    /**
     * * 定义 路径事件
     */
    const TickEvent = (args: data_format.ROUTE_ANNEX_ARGS) => {
        /**
         * * 获取 方块
         */
        const getBlock = args.dimension.getBlock(args.location);
        // 检测 方块是否存在
        if (!getBlock || !getBlock.isValid()) return false;
        /**
         * * 获取 方块类型
         */
        const protoResult = data_block.is_crops.get(getBlock.typeId);
        /**
         * * 获取 方块状态
         */
        const getPermutation = getBlock.permutation;
        // 如果 可以收割
        if (protoResult === true || getPermutation.getState('growth') == 7) return false;
        else if (getBlock.typeId == 'minecraft:sweet_berry_bush' && getPermutation.getState('growth') == 3) return false;
        else if (getBlock.typeId == 'minecraft:cocoa' && getPermutation.getState('age') == 2) return false;
        else if (getBlock.typeId == 'nether_wart' && getPermutation.getState('age') == 3) return false;
        // 继续循环
        else return true;
    };
    /**
     * * 定义 停止事件
     */
    const StopEvent = (args: data_format.ROUTE_ANNEX_ARGS) => {
        if (args.tick > 15) return;
        /**
         * * 获取 方块
         */
        const getBlock = object.north();
        if (!getBlock || !getBlock.isValid()) return;
        /**
         * * 获取 方块状态
         */
        const getPermutation = getBlock.permutation;
        try { getBlock.setPermutation(getPermutation.withState('STATE:rune_type', math_tools.Random({ max: 7, min: 1 }, true))) } catch { }
    };
    // 创建 路径事件
    achieve_plan.PathExecute.Create('作物侦测-检测射线', 1,
        {
            location_group: [object, math_tools.Vector.add(object, { x: 0, y: 0, z: 15 })],
            particles: ['constant:track_rune_green'],
            offset: { x: 0.5, y: 0.5, z: 0.5 },
            dimension: object.dimension,
            on_move: TickEvent,
            on_done: StopEvent,
            cooldown: 1,
            speed: 1
        }
    );
};